package com.zdy.order.service;

import com.zdy.feign.clients.UserClient;
import com.zdy.feign.pojo.User;
import com.zdy.order.mapper.OrderMapper;
import com.zdy.order.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserClient userClient;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);

      /*  //2.利用RestTemplate 发送http请求，查询用户
        //2.1 url路径
        String url = "http://userservice/user/" + order.getUserId();
        //2.2 发送http请求，实现远程调用
        User user = restTemplate.getForObject(url, User.class);*/

        //利用feign的方式进行路径请求
        User user = userClient.findById(order.getUserId());
        //3.将查询到的user对象封装到order对象中
        order.setUser(user);
        // 4.返回
        return order;
    }
}
